package com.open.rbac.service.sys.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.sys.OrganizationUserRel;

/**
 * 组织机构与用户关系
 *
 * @author Riche's
 * @since 2025/9/2
 */
@Repository
public interface OrganizationUserRelRepository extends JpaRepository<OrganizationUserRel, String> {

    /**
     * 按用户Id物理删除'组织机构与用户关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM organization_user_rel rel WHERE rel.userId = ?1")
    void deleteReallyByUserId(String userId);

    /**
     * 按组织机构Id物理删除'组织机构与用户关系'
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM organization_user_rel rel WHERE rel.organizationId = ?1")
    void deleteReallyByOrganizationId(String organizationId);

    /**
     * 按组织机构Id列表查询用户Id列表
     */
    @Query("SELECT DISTINCT t1.userId FROM organization_user_rel t1 WHERE t1.organizationId IN (?1)")
    List<String> findUserIds(List<String> organizationIds);


    @Query("SELECT t1 FROM organization_user_rel t1 WHERE t1.userId IN (?1)")
    List<OrganizationUserRel> findByUserIds(List<String> userIds);

}
